<template>
    <div class="app-container">
        <el-form :model="queryParams" ref="queryForm" :inline="true" v-show="showSearch" label-width="68px">
            <el-form-item label="部门名称" prop="depName">
                <el-input
                        v-model="queryParams.depName"
                        placeholder="请输入角色名称"
                        clearable
                        style="width: 240px;"
                        size="small"
                />
            </el-form-item>
            <el-form-item label="状态" prop="state" >
                <el-select v-model="queryParams.state" size="small">
                    <el-option
                            v-for="dicnars in dataStatesOptions"
                            :key="dicnars.dataType"
                            :label="dicnars.dataName"
                            :value="dicnars.dataType"
                    >
                    </el-option>
                </el-select>
            </el-form-item>
            <el-form-item>
                <el-button type="primary" icon="el-icon-search" size="mini" @click="getList"
                           v-has="'basic:department:page'">搜索</el-button>
                <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
            </el-form-item>
        </el-form>

        <el-row :gutter="10" class="mb8">
            <el-col :span="1.5">
                <el-button
                        type="primary"
                        size="small"
                        icon="el-icon-plus"
                        @click="save(null)"
                        v-has="'basic:department:add'"
                >新增</el-button>
            </el-col>
        </el-row>
        <el-table v-loading="loading" :data="list">
            <el-table-column label="部门表id" align="center" prop="id" v-if="false"/>
            <el-table-column label="部门名称" align="center" prop="depName" />
            <el-table-column label="状态" :formatter="dicdataFormate" align="center" prop="state" />
            <el-table-column label="创建时间" align="center" prop="createTime" />
            <el-table-column label="创建人" align="center" prop="creatar" />
            <el-table-column label="更新时间" align="center" prop="updateTime"/>
            <el-table-column label="更新人" align="center" prop="updataer"/>
            <el-table-column label="备注" align="center" prop="remark"/>
            <el-table-column label="操作" align="center" class-name="small-padding fixed-width" >
                <template slot-scope="scope">
                    <el-button
                            size="mini"
                            type="text"
                            icon="el-icon-edit"
                            @click="save(scope.row)"
                            v-has="'basic:department:update'"
                    >修改</el-button>
                    <el-button
                            size="mini"
                            type="text"
                            icon="el-icon-delete"
                            @click="remove(scope.row)"
                            v-has="'basic:department:delete'"
                    >删除
                    </el-button>
                </template>
            </el-table-column>
        </el-table>
        <pagination
                v-show="total>0"
                :total="total"
                :page.sync="queryParams.pageNum"
                :limit.sync="queryParams.pageSize"
                @pagination="getList"
        />


        <!-- 添加或修改参数配置对话框 -->
        <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
            <el-form :model="form" :rules="rules" ref="loginForm" class="login-form" label-width="108px">
                <el-form-item label="部门名称" prop="depName">
                    <el-input v-model="form.depName"/>
                </el-form-item>
                <el-form-item label="状态" prop="state">
                    <el-radio-group v-model="form.state">
                        <el-radio v-for="datastate in dataStatesOptions"
                                  :key="datastate.dataType"
                                  :label="datastate.dataType">
                            {{datastate.dataName}}
                        </el-radio>
                    </el-radio-group>
                </el-form-item>
                <el-form-item label="备注" prop="remark">
                    <el-input type="textarea" v-model="form.remark"/>
                </el-form-item>
            </el-form>
            <div slot="footer" class="dialog-footer">
                <el-button type="primary" @click="submitForm">确 定</el-button>
                <el-button @click="close">取 消</el-button>
            </div>
        </el-dialog>
    </div>
</template>

<script>
    import {departmentpage,getDepartment,addDepartment,delDepartment,updateDepartment} from "@/api/user/department";
    export default {
        name: "Department",
        data() {
            return {
                title:'查看详情',
                open:false,
                // 遮罩层
                loading: false,
                // 显示搜索条件
                showSearch: true,
                // 总条数
                total: 0,
                // 表格数据
                list: [],
                // 表单参数
                form: {
                    id: null,
                    state:'1',
                },
                // 日期范围
                dateRange: [],
                //状态字典
                dataStatesOptions:[],
                // 查询参数
                queryParams: {
                    pageNum: 1,
                    pageSize: 10,
                    depName:null,
                    state:null,
                },
                rules: {
                    roleName:[
                        { required: true, message: '请输入角色名称', trigger: 'blur' },
                        { max: 32, message: '长度在 32 个字符', trigger: 'blur' }
                    ],
                    roleCode:[
                        { required: true, message: '请输入角色标识', trigger: 'blur' },
                        { max: 32, message: '长度在 32 个字符', trigger: 'blur' }
                    ],
                },
            };
        },
        created() {
            this.getList();
            this.getDicdataByType("stats_data").then(response => {
                this.dataStatesOptions=response.data;
            });
        },
        methods: {

            /**
             * 状态字段格式化
             * */
            dicdataFormate(row) {
                return this.selectLabel(this.dataStatesOptions, row.state);
            },
            /** 查询登录日志列表 */
            getList() {
                this.loading = true;
                departmentpage(this.queryParams).then(response => {
                        this.list = response.data.records;
                        this.total = response.data.total;
                        this.loading = false;
                    }
                );
            },
            save(row){
                this.reset();
                if(row!=null){
                    getDepartment(row.id).then(response => {
                        this.title = '修改部门';
                        this.open=true;
                        this.form=response.data;
                    });
                }else{
                    this.title = '新增部门';
                    this.open=true;
                }
            },
            /** 重置按钮操作 */
            resetQuery() {
                this.resetForm("queryForm");
                this.queryParams.pageNum = 1;
            },
            submitForm() {
                this.$refs["loginForm"].validate(valid => {
                    if (valid) {
                        if (this.isEmpty(this.form.id)) {
                            addDepartment(this.form).then(response => {
                                if (response.code == 0) {
                                    this.msgSuccess("新增成功");
                                    this.open = false;
                                    this.getList();
                                }else{
                                    this.msgError(response.msg);
                                }
                            })
                        } else {
                            updateDepartment(this.form).then(response => {
                                if (response.code == 0) {
                                    this.msgSuccess("修改成功");
                                    this.open = false;
                                    this.getList();
                                }else{
                                    this.msgError(response.msg);
                                }
                            })
                        }
                    }
                });
            },
            reset() {
                // 表单参数
                this.form = {
                    id: null,
                    state:'1',
                    roleName: null,
                    roleCode:null,
                    remark: null,
                };
                this.resetForm("form");
            },
            close(){
                this.title = '';
                this.open = false;
                this.resetForm("form");
            },
            remove(row){
                delDepartment(row.id).then(response => {
                    if (response.code == 0) {
                        this.msgSuccess("删除成功");
                        this.getList();
                    }else{
                        this.msgError(response.msg);
                    }
                });
            }
        }
    };
</script>

<style scoped>
</style>